<template>
  <div>
    <!-- 
          el-dialog
          center	是否对头部和底部采用居中布局	boolean
       -->
    <el-dialog
      center
      :title="isAdd ? '添加菜单' : '编辑菜单'"
      :visible.sync="isShow"
      :before-close="cancel"
    >
      <el-form :model="spglList" ref="cateInfo" :rules="rules">
        <el-form-item
          label="一级分类"
          :label-width="formLabelWidth"
          prop="title"
        >
          <el-select
            v-model="spglList.first_cateid"
            placeholder="请选择"
            @change="changeCate"
          >
            <el-option
              v-for="item in spec"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="二级分类"
          :label-width="formLabelWidth"
          prop="title"
        >
          <el-select v-model="spglList.second_cateid" placeholder="请选择">
            <el-option
              v-for="item in reclassify"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="商品名称"
          :label-width="formLabelWidth"
          prop="title"
        >
          <el-input v-model="spglList.goodsname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="价格" :label-width="formLabelWidth" prop="title">
          <el-input v-model="spglList.price" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="市场价格"
          :label-width="formLabelWidth"
          prop="title"
        >
          <el-input
            v-model="spglList.market_price"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="图片"
          :label-width="formLabelWidth"
          
        >
          <el-upload
            action="#"
            list-type="picture-card"
            :on-preview="handlePictureCardPreview"
            :on-change="handleRemove"
            :auto-upload="false"
            :file-list="filelist"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="" />
          </el-dialog>
        </el-form-item>

        <el-form-item
          label="商品规格"
          :label-width="formLabelWidth"
          prop="title"
        >
          <el-select v-model="spglList.specsid" placeholder="请选择"
          @change="changeSpecs"
          >
            <el-option
              v-for="item in specs"
              :key="item.id"
              :label="item.specsname"
              :value="item.id"
             
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item
          label="规格属性"
          :label-width="formLabelWidth"
          prop="title"
        >
          <el-select v-model="spglList.specsattr" placeholder="请选择"
          multiple="multiple"
          >
            <el-option
              v-for="item in ggsxL"
              :key="item"
              :label="item"
              :value="item"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item
          label="是否新品"
          :label-width="formLabelWidth"
          prop="title"
        >
        </el-form-item>
        <el-form-item
          label="是否热卖"
          :label-width="formLabelWidth"
          prop="title"
        >
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="spglList.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="add">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { indexRoutes } from "../../router/index";
import { getGoodsAdd, getGoodsInfo, getGoodsEdit } from "../../request/api";
import { mapActions, mapGetters } from "vuex";
export default {
  props: ["isShow", "isAdd"],
  data() {
    return {
      dialogImageUrl: "",
      dialogVisible: false,
      rules: {
        rolename: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 2 到 8 个字符", trigger: "blur" },
        ],
      },
      spglList: {
        first_cateid: 0, //一级分类编号
        second_cateid: 0, //二级分类编号
        goodsname: "", //商品名称
        price: 0, //商品价格
        market_price: 0, //市场价格
        img: "", //商品图片（文件）
        description: "", //商品描述
        specsid: 0, //商品规格编号
        specsattr: "", //商品规格属性
        isnew: 1, //是否新品     1-是 2-否
        ishot: 1, //是否热卖推荐 1-是 2-否
        status: 1, //状态1正常2禁用
      },
      defaultProps: {
        children: "children",
        label: "title",
      },
      formLabelWidth: "120px",
      indexRoutes: indexRoutes,
      filelist: [],
      reclassify: [], //二级分类数组
      ggsxL:[]//规格属性
    };
  },
  computed: {
    ...mapGetters({
      spec: "spec/getcate", //一级分类菜单列表
      specs: "specs/getSpecsList", //规格列表
    }),
  },

  mounted() {
    this.getSpec();
    this.getSpecs();
  },
  methods: {
    //  //封装一个一级分类切换渲染二级分类规格列表
    changeSpecs() { 
      console.log(this.specs)
      let index = this.specs.findIndex((item) => {
        return item.id == this.spglList.specsid;
      });
     this.ggsxL= this.specs[index].attrs
    
    },
    // //封装一个一级分类切换渲染二级分类列表事件
    changeCate() {
      let index = this.spec.findIndex((item) => {
        return item.id == this.spglList.first_cateid;
      });
      console.log(index, "下标索引");
      this.reclassify = this.spec[index].children;

      this.spglList.second_cateid = "";
    },
    //
    ...mapActions({
      getSpec: "spec/getCateList",
      getSpecs: "specs/getSpecsListAction",
      getSpgl:'spgl/getSpglListAction'
    }),
    handleRemove(file) {
      console.log(file);
      this.spglList.img = file.raw;
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
    },

    // 清空表单
    reset() {
      (this.spglList = {
       
      }),
        this.$refs.cateInfo.resetFields();
      //图片数组清空
      this.filelist = [];
    },
    //封装一个取消事件
    cancel() {
      this.$emit("cancel", false);
      this.reset();
    },
    // 封装一个事件
    lookup(id) {
      console.log(id, 111111111111111);
      //  获取一条分类数据
      getGoodsInfo({ id }).then((res) => {
        if (res.code == 200) {
          console.log(res);
          this.spglList = res.list;
          this.spglList.id = id;
          this.filelist = [{ url: this.$img + this.spglList.img }];
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    add() {
      this.$refs.cateInfo.validate((val) => {
        if (val) {
          if (this.isAdd) {
            // 商品添加
            console.log(111111111111222222222);
            getGoodsAdd(this.spglList).then((res) => {
              console.log(res);
              if (res.code == 200) {
                this.$message.success(res.msg);
                this.getSpgl();
                this.cancel();
              } else {
                this.$message.error(res.msg);
              }
            });
          } else {
            getGoodsEdit(this.spglList).then((res) => {
              console.log(res);
              if (res.code == 200) {
                this.$message.success(res.msg);

                this.cancel();
                this.getSpgl();
              } else {
                this.$message.error(res.msg);
              }
              this.cancel();
            });
          }
        }
      });
    },
  },
};
</script>

<style scoped></style>
